﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TBlog.Web.Service;
using TBlog.Web.ViewModel;
using TBlog.Web.Common.Authentication;
namespace TBlog.Web.Controllers
{
    public class ManageController : Controller
    {
        //
        // GET: /Manage/
        IFormsAuthentication websetcookie;// = new WebAuthentication();
        readonly IManageService _mservice;
        public ManageController(IManageService mservice, IFormsAuthentication _websetcookie)
        {
            _mservice = mservice;
            websetcookie = _websetcookie;
        }
        public ActionResult Login()
        {
            //Session["PwdErrorCount"] = "3";
            if (HttpContext.User.Identity.IsAuthenticated)
            {
                return RedirectToAction("GetListArticle", "Article");
            }
            var cookiename = Request.Cookies["UserName"];
            if (cookiename != null)
            {
                ViewBag.UserName = cookiename;
            }
            return View();
        }
        [ValidateAntiForgeryToken]
        [AllowAnonymous]
        [HttpPost]
        public JsonResult Login(Manage model)
        {
            int errorcount = Session["PwdErrorCount"] == null ? 0 : Convert.ToInt32(Session["PwdErrorCount"]);

            //验证密码是否正确
            if (!string.IsNullOrWhiteSpace(model.ValidCode))
            {
                if (model.ValidCode == Session["VerifyCode"].ToString())
                    return Json(_mservice.GetJsonMessage(false, "", "验证码错误，请重新输入"));
            }
            var res = _mservice.Valid(model.UserName, model.PassWord);
            if (!res)
            {
                errorcount += 1;
                Session["PwdErrorCount"] = errorcount;
                return Json(_mservice.GetJsonMessage(false, "", "出现错误"), JsonRequestBehavior.DenyGet);
            }
            Session["PwdErrorCount"] = 0;

            if (model.IsRemember)
            {
                HttpCookie cookie = new HttpCookie("UserName");
                cookie.Value = model.UserName;
                cookie.Expires = DateTime.Now.AddDays(5);
                Response.Cookies.Set(cookie);
            }
            websetcookie.SetAuthenticationToken(model.UserName, true);
            return Json(_mservice.GetJsonMessage(true, "", "正常"));

        }

        /// <summary>
        /// 退出登录
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult LoginOut()
        {
            websetcookie.SignOut();
            return RedirectToAction("Login", "Manage");
            //return Json(_mservice.GetJsonMessage(true, "", "退出登录"),JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 生成验证码
        /// </summary>
        /// <returns></returns>
        public ActionResult GetValidateCode()
        {
            byte[] bytes = _mservice.GetValidate(5);
            return File(bytes, @"image/jpeg");
        }




        [Authorize]
        public ActionResult GetListComment()
        {
            return View("CommentList");
        }
    }
}
